package virtPresenter.model
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import mx.controls.SWFLoader;

	/**
	 * This is a singletonClass representing a fisheye object.
	 * 
	 * @author: Sebastian Pospiech
	 */
	public class FisheyeWindow
	{
		private static var instance:FisheyeWindow;
				
		private var previewLeftProxy:ISlideWrapperProxy = new SlideWrapperProxy("presentationHoldera.swf","swf8connectora", "swf2FlexA");
		private var previewMiddleProxy:ISlideWrapperProxy = new SlideWrapperProxy("presentationHolderb.swf","swf8connectorb", "swf2FlexB");
		private var previewRightProxy:ISlideWrapperProxy = new SlideWrapperProxy("presentationHolderc.swf","swf8connectorc", "swf2FlexC");
		
		private var previewLeftSprite:SWFLoader;
		private var previewMiddleSprite:SWFLoader;
		private var previewRightSprite:SWFLoader;
		
		private var previewMode:Boolean = false;
		private var textLeft:TextField;
		private var textRight:TextField;
		
		private var overlayLeft:MovieClip;
		private var overlayRight:MovieClip;
		
		public function FisheyeWindow(enforcer:Class)
		{
			if (enforcer!=SingletonEnforcer)
				throw new Error("This is a Singleton class. Please use getInstance() method!");
			virtPresenterLocator.getInstance().previewLeftProxy = previewLeftProxy;
			virtPresenterLocator.getInstance().previewMiddleProxy = previewMiddleProxy;
			virtPresenterLocator.getInstance().previewRightProxy = previewRightProxy;
		}
		
		public static function getInstance():FisheyeWindow{
			if (instance==null) 
				instance = new FisheyeWindow(SingletonEnforcer);
			return instance;
		}
		
		public function getPreviewMode():Boolean{
			return this.previewMode;
		}
		
		public function setPreviewMode(activated:Boolean):void{
			this.previewMode = activated;
		}
		
		public function initializeFisheyes(sprite:Sprite):void{
			previewLeftSprite = previewLeftProxy.getSWF();
			previewLeftSprite.height=90;
			previewLeftSprite.width=130;
			
			previewMiddleSprite = previewMiddleProxy.getSWF();
			previewMiddleSprite.height= AS_Constants.HEIGHT_PREVIEW_MIDDLE;
			previewMiddleSprite.width = 150;
			previewMiddleSprite.visible = true;
			
			previewRightSprite = previewRightProxy.getSWF();
			previewRightSprite.height=90;
			previewRightSprite.width=130;
			
			sprite.addChild(previewLeftSprite);
			sprite.addChild(previewMiddleSprite);
			sprite.addChild(previewRightSprite);
			
			textLeft = new TextField();
			textRight = new TextField();
			initializeTextField(textLeft);
			initializeTextField(textRight);
			
			overlayLeft = new MovieClip();
			overlayRight = new MovieClip();
			initializePreviewOverLay(previewLeftSprite, overlayLeft, textLeft);
			initializePreviewOverLay(previewRightSprite, overlayRight, textRight);
		}
		
		public function initializeTextField(text:TextField):void{
			var format:TextFormat = new TextFormat();
			format.size = 24;
			format.bold = true;
			format.align = TextFormatAlign.CENTER;
			
			text.autoSize = TextFieldAutoSize.CENTER;
			text.antiAliasType = AntiAliasType.ADVANCED;
			text.selectable = false;
			text.mouseEnabled = false;
			text.defaultTextFormat = format;
			text.text = "Preview";
			text.y = previewLeftSprite.height/2 - text.textHeight/2;
			text.x = previewLeftSprite.width/2 - text.textWidth/2;
		}
		
		public function getMiddleSprite():Sprite{
			return previewMiddleSprite;
		}
		
		public function drawFisheyes(thumbX:int, x:Number, leftIsVisible:Boolean, rightIsVisible:Boolean):void{
			resetSideFisheyes();
			if (leftIsVisible) drawPreviewLeft(thumbX,x);
			drawPreviewMiddle(thumbX,x);
			if (rightIsVisible) drawPreviewRight(thumbX,x);
		}
		
		private function resetSideFisheyes():void{
			
			previewLeftSprite.visible = previewRightSprite.visible = false;
			overlayLeft.visible = overlayRight.visible = false;
		}
		
		private function initializePreviewOverLay(parent:Sprite, sprite:Sprite, text:TextField):void{
			sprite.graphics.beginFill(0x8f8f91,0.6);
			sprite.graphics.drawRect(-5,-10,AS_Constants.WIDTH_PREVIEW_SIDES,AS_Constants.HEIGHT_FISHEYE_SIDES_BORDER);
			sprite.graphics.endFill();
			sprite.addChild(text);
			parent.addChild(sprite);
		}
		
		private function drawPreviewLabel(sprite:Sprite, text:TextField):void{
			
		}
		
		private function drawPreviewLeft(thumbX:int, x:Number):void{
			previewLeftSprite.graphics.clear();
			previewLeftSprite.graphics.lineStyle(2,0x000000);
			previewLeftSprite.graphics.drawRect(-5,-10,AS_Constants.WIDTH_PREVIEW_SIDES,AS_Constants.HEIGHT_FISHEYE_SIDES_BORDER);
			previewLeftSprite.x=thumbX-195-x;
			previewLeftSprite.y=AS_Constants.SCRUBBER_HEIGHT + AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER+10;
			previewLeftSprite.cacheAsBitmap = true;
			previewLeftSprite.visible = true;
			if (previewMode){
				overlayLeft.visible = true;
			}
		}
		
		private function drawPreviewMiddle(thumbX:int, x:Number):void{
			previewMiddleSprite.graphics.clear();
			previewMiddleSprite.graphics.lineStyle(2,0x000000);
			previewMiddleSprite.graphics.drawRect(-3,-10,AS_Constants.WIDTH_PREVIEW_MIDDLE,AS_Constants.HEIGHT_FISHEYE_MIDDLE_BORDER);		
			previewMiddleSprite.x=thumbX-67-x;
			previewMiddleSprite.cacheAsBitmap = true;
			previewMiddleSprite.y=AS_Constants.SCRUBBER_HEIGHT + AS_Constants.DISTANCE_PREVIEW_MIDDLE_TO_SCRUBBER+10;
		}
		
		private function drawPreviewRight(thumbX:int, x:Number):void{
			previewRightSprite.graphics.clear();
			previewRightSprite.graphics.lineStyle(2,0x000000);
			previewRightSprite.graphics.drawRect(-5,-10,AS_Constants.WIDTH_PREVIEW_SIDES,AS_Constants.HEIGHT_FISHEYE_SIDES_BORDER);
			previewRightSprite.x=thumbX+75-x;
			previewRightSprite.y=AS_Constants.SCRUBBER_HEIGHT + AS_Constants.DISTANCE_PREVIEW_SIDES_TO_SCRUBBER+10;
			previewRightSprite.cacheAsBitmap = true;
			previewRightSprite.visible = true;
			if (previewMode){
				overlayRight.visible = true;
			}
		}
	}
}

class SingletonEnforcer{}